from tkinter import Label, Checkbutton, Entry, END, Button, Text, BooleanVar
from tkinter import W,E,S,N
from tkinter import colorchooser
from tkinter import filedialog


class CustomLabelPair:

    def __init__(self, win, label_name, r, c=1, label_data="", left_width=10, left_anchor='e', right_col=3, right_anchor='w', sticky=W):
        """
        文本标签对，仅显示数据
        :param win: tk_Frame对象容器
        :param label_name: 标签名
        :param r: 排第几行
        :param label_data: 标签数据
        :param left_width: 左标签宽度
        :param left_anchor: 左标签对齐方式，默认为右对齐。
        :param right_anchor: 右标签对齐方式，默认为左对齐。
        :param right_col: 右标签占栏数，默认为3。
        :param sticky: 控件整体排布grid对齐方式，默认为左对齐。
        """
        self.label_root = win
        self.left_label = Label(master=self.label_root, text=label_name + "：", width=left_width, anchor=left_anchor)
        self.left_label.grid(row=r, column=c, sticky=sticky, columnspan=1)
        self.right_label = Label(master=self.label_root, text=label_data, width=left_width*right_col, anchor=right_anchor, wraplength=300)
        self.right_label.grid(row=r, column=c+1, sticky=sticky, columnspan=right_col)

    def fresh_data(self, label_data):
        """
        在右侧部分，刷新显示对应数据
        :param label_data: 新数据
        :return:
        """
        self.right_label['text'] = label_data


class CustomLabelCheckbox:
    def __init__(self, win, label_name, r, left_width=10, left_anchor='e', right_anchor='w', sticky=W, check_num=1, label_data={}):
        """
        多选标签对，仅显示数据
        :param win: tk_Frame对象容器
        :param label_name: 标签名
        :param r: 排第几行
        :param left_width: 左标签宽度
        :param left_anchor: 左标签对齐方式，默认为右对齐。
        :param right_anchor: 右标签对齐方式，默认为左对齐。
        :param sticky: 控件整体排布grid对齐方式，默认为左对齐。
        """
        self.label_root = win
        self.left_label = Label(master=self.label_root, text=label_name + "：", width=left_width, anchor=left_anchor)
        self.left_label.grid(row=r, column=1, sticky=sticky, columnspan=1)

        if check_num > 3:
            raise Exception("CustomeLabelCheckbox控件最多只支持三个勾选项")
        if check_num > 2:
            self.v3 = BooleanVar()
            self.check3 = Checkbutton(master=self.label_root, text="", width=left_width, anchor=right_anchor, variable=self.v3)
            self.check3.grid(row=r, column=4, sticky=sticky, columnspan=1)
        if check_num > 1:
            self.v2 = BooleanVar()
            self.check2 = Checkbutton(master=self.label_root, text="", width=left_width, anchor=right_anchor, variable=self.v2)
            self.check2.grid(row=r, column=3, sticky=sticky, columnspan=1)
        if check_num > 0:
            self.v1 = BooleanVar()
            self.check1 = Checkbutton(master=self.label_root, text="", width=left_width, anchor=right_anchor, variable=self.v1)
            self.check1.grid(row=r, column=2, sticky=sticky, columnspan=1)

        if label_data != {}:
            self.fresh_data(check_dict=label_data, check_num=len(label_data))

    def fresh_data(self, check_dict=None, check_num=1):
        """
        根据布尔值显示勾选状态
        :param check_dict: 列表类型，text值为显示文字，value值为勾选值
               格式：[{"text": "显示文字1","value": True},{"text": "显示文字2","value": False}]
        :param check_num: 勾选项数量
        :return:
        """
        if check_num > 3 or len(check_dict) > 3:
            raise Exception("CustomeLabelCheckbox控件最多只支持三个勾选项")
        if check_num > 2 or len(check_dict) > 2:
            self.check3['text'] = check_dict[2]['text']
            if check_dict[2]['value']:
                self.check3.select()
            else:
                self.check3.deselect()
        if check_num > 1 or len(check_dict) > 2:
            self.check2['text'] = check_dict[1]['text']
            if check_dict[1]['value']:
                self.check2.select()
            else:
                self.check2.deselect()
        if check_num > 0 or len(check_dict) > 0:
            self.check1['text'] = check_dict[0]['text']
            if check_dict[0]['value']:
                self.check1.select()
            else:
                self.check1.deselect()


class CustomLabelPairEdit:

    def __init__(self, win, label_name, r, c=1, left_width=10, sticky=W, left_anchor='e', right_text="", right_col=3, label_data=""):
        """
        可编辑标签对
        :param win:
        :param label_name:
        :param r:
        :param c:
        :param left_width:
        :param sticky:
        :param left_anchor:
        :param right_text:
        :param right_col:
        :param label_data:
        """
        self.label_root = win
        self.left_label = Label(master=self.label_root, text=label_name + "：", width=left_width, anchor=left_anchor)
        self.left_label.grid(row=r, column=c, sticky=sticky, columnspan=1)

        self.right_input = Entry(master=self.label_root, width=left_width*right_col, text=right_text)
        self.right_input.grid(row=r, column=c+1, sticky=W, columnspan=right_col)

        if label_data != "":
            self.fresh_data(label_data)

    def fresh_data(self, show_data):
        self.right_input.insert(END, show_data)


class CustomColorPicker:

    def __init__(self, win, label_name, r, c=1, left_width=10, sticky=W, left_anchor='e', bg_color="#FFFFFF"):
        """
        颜色选择器
        :param win:
        :param label_name:
        :param r:
        :param c:
        :param left_width:
        :param sticky:
        :param left_anchor:
        :param bg_color:
        """
        self.color_picker_root = win
        self.left_label = Label(master=self.color_picker_root, text=label_name + "：", width=left_width, anchor=left_anchor)
        self.left_label.grid(row=r, column=c, sticky=sticky, columnspan=1)

        self.color_picker = Button(self.color_picker_root, text="选择颜色", anchor='center', command=self.color_pick, width=left_width)
        self.color_picker.grid(row=r, column=c+1, sticky=sticky, columnspan=1)

        self.color_show_img = Label(master=self.color_picker_root, width=left_width, background=bg_color)
        self.color_show_img.grid(row=r, column=c+2, sticky=sticky, columnspan=1)

    def color_pick(self):
        """选择颜色"""
        self.color_show = colorchooser.askcolor()
        # 返回值格式(RGB,16位表示值)
        # ((255.99609375, 255.99609375, 255.99609375), '#ffffff')

        choose_color = self.color_show[-1]
        if choose_color:
            self.color_show_img['background'] = choose_color

    def choose_color(self):
        return self.color_show_img['background']


class CustomFilePicker:

    def __init__(self, win, label_name, r, c=1, sticky=W, left_width=10, left_anchor='e', label_data=""):
        """
        路径选择器
        :param win: 父元素
        :param label_name: 左侧标签名
        :param r: 第几行
        :param c: 第几列，默认1
        :param sticky: 对齐，默认W
        :param left_width: 左栏宽度，默认10
        :param left_anchor: 左栏文字对齐方式，默认'e'右对齐
        :param label_data: 右栏显示数据，默认为空
        """
        self.file_root = win
        self.left_label = Label(master=self.file_root, text=label_name + "：", width=left_width, anchor=left_anchor)
        self.left_label.grid(row=r, column=c, sticky=sticky, columnspan=1)

        self.file_picker = Button(master=self.file_root, text="修改路径", anchor='center', command=self.find_dir_change, width=left_width)
        self.file_picker.grid(row=r, column=c+1, sticky=sticky, columnspan=1)

        self.new_path_label = Label(master=self.file_root, text=label_data, anchor='w', wraplength=300)
        self.new_path_label.grid(row=r+1, column=c+1, sticky=sticky, columnspan=3)

    def find_dir_change(self):
        """选择路径，不限次数"""
        choose_dir = filedialog.askdirectory()
        if choose_dir:
            self.new_path_label['text'] = choose_dir

    def choose_path(self):
        """返回界面上的路径"""
        return self.new_path_label['text']


class CustomLabelText:
    def __init__(self, win, label_name, r, c=1, sticky=W, left_width=10, left_anchor='e', right_col=3):
        """

        :param win:
        :param label_name:
        :param r:
        :param c:
        :param sticky:
        :param left_width:
        :param left_anchor:
        :param right_col:
        """
        self.text_root = win
        self.left_label = Label(master=self.text_root, text=label_name + "：", width=left_width, anchor=left_anchor)
        self.left_label.grid(row=r, column=c, sticky=sticky, columnspan=1)

        self.right_text = Text(master=self.text_root, width=30, height=2, background="#F5F5F5", font=20)
        self.right_text.grid(row=r, column=c+1, sticky=sticky, columnspan=right_col)
